package bytecode.annotation;

import bytecode.Type.TypeTest;
import bytecode.proxy.statics.Hello;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@StudentInfo(age = 13, name = "sun")
public class TestInfo extends TypeTest implements Hello {

    List<String> list = new ArrayList<>();
    String[] array = new String[10];

    @StudentInfo(age = 13, name = "sun")
    String address = "xian";

    @StudentInfo(age = 0, name = "baby")
    public void born(String name) {
        System.out.println("human born ");

    }


    public static void main(String[] args) throws Exception {


        TestInfo ti = new TestInfo();
        System.out.println(ti.getClass().getAnnotatedSuperclass().getType().getTypeName());
        for (AnnotatedType a : ti.getClass().getAnnotatedInterfaces()) {
            System.out.println(a.getType().getTypeName());
        }
        // 数组
        Field list = TestInfo.class.getDeclaredField("list");
        Field array = TestInfo.class.getDeclaredField("array");
        // 不是
        System.out.println(list.getType().getTypeName() + " is array :" + list.getType().isArray());
        // 是
        System.out.println(array.getType().getTypeName() + " is array :" + array.getType().isArray());

        Object arr = Array.newInstance(String.class, 10);
        Array.set(arr, 0, "demo");
        System.out.println(Array.get(arr, 0));


        // 方法
        ti.born("ok");

        Method born = TestInfo.class.getDeclaredMethod("born", String.class);

        TestInfo.class.newInstance();//
        TestInfo.class.getConstructor(String.class, Integer.class).newInstance();


        System.out.println(born.getParameterTypes()[0]);
        Method signatureget = Method.class.getDeclaredMethod("getGenericSignature");
        signatureget.setAccessible(true);
        System.out.println(signatureget.invoke(born));

        if (born.isAnnotationPresent(StudentInfo.class)) {
            StudentInfo info = born.getAnnotation(StudentInfo.class);
            System.out.println(info.age());
            System.out.println(info.name());
            System.out.println(info.descriptor());
        }
        // 获取注解
        Field address = TestInfo.class.getDeclaredField("address");
        // get 这里是String 对象，如果是int还可以使用getInt(ti)
        String addressValue = (String) address.get(ti);
        System.out.println(addressValue);
        // 基本信息
        System.out.println(address.toString());
        // 注解信息
        System.out.println(address.getAnnotations()[0].toString());
        // 类型名
        System.out.println(address.getAnnotatedType().getType().getTypeName());


    }
}
